package 贪心算法;

public class 跳跃游戏2 {

    public static int jump(int[] nums) {
        int lastMax = 0;    //上一次跳跃的最远距离
        int curMax = nums[0];     //此次跳跃的最远距离
        int times = 1; //跳跃次数
        int n = nums.length;
        if (n <= 1){
            return 0;
        }
        while (curMax < n -1){
            times++;
            int max = curMax;
            curMax += nums[curMax];
            if (curMax >= n-1){
                return times;
            }
            for (int i = lastMax+1; i < max; i++) {
                curMax = Math.max(i+nums[i],curMax);
            }
            lastMax = max;
        }
        return times;
    }
    public  static int jump1(int[] nums) {
        int position = nums.length - 1; //要找的位置
        int steps = 0;
        while (position != 0) { //是否到了第 0 个位置
            for (int i = 0; i < position; i++) {
                if (nums[i] >= position - i) {
                    position = i; //更新要找的位置
                    steps++;
                    break;
                }
            }
        }
        return steps;
    }


    public static void main(String[] args) {
        int[] nums = {0};
        System.out.println(jump(nums));
    }
}
